//
//  BehindersViewController.m
//  Behinders
//
//  Created by sigure on 14-10-28.
//  Copyright (c) 2014年 sigure. All rights reserved.
//

#import "DiscoverViewController.h"
#import "UIView+Extension.h"
#import "musicanInfo.h"
#import "NewViewController.h"
#define DIAMETER 80
#define DIS ((MAINWIDTH-(DIAMETER*3))/4)
#define imageY 130

@interface DiscoverViewController ()
@property (nonatomic, strong) NSArray *musicanList;
@end

@implementation DiscoverViewController

- (NSArray *)musicanList
{
    if (!_musicanList) {
        // 从mainBundle加载
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"musican.plist" ofType:nil];
        
        NSArray *array = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *arrayM = [NSMutableArray array];
        
        for (NSDictionary *dict in array) {
            [arrayM addObject:[musicanInfo musicanWithDict:dict]];
        }
        
        _musicanList = arrayM;
    }
    return _musicanList;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    
    
    
    UITextField *searchBar = [[UITextField alloc] init];
    searchBar.width = 300;
    searchBar.height = 30;
    
    // 设置背景
    searchBar.background = [UIImage resizedImage:@"searchbar_textfield_background"];
    
    // 设置内容 -- 垂直居中
    searchBar.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    // 设置左边显示一个放大镜
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageWithName:@"searchbar_textfield_search_icon"];
    leftView.width = searchBar.height;
    leftView.height = searchBar.height;
    // 设置leftView的内容居中
    leftView.contentMode = UIViewContentModeCenter;
    searchBar.leftView = leftView;
    
    // 设置左边的view永远显示
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    // 设置右边永远显示清除按钮
    searchBar.clearButtonMode = UITextFieldViewModeAlways;
    // 设置预设文字
    searchBar.Placeholder = @"搜索角色、姓名、音乐";
    
    self.navigationItem.titleView = searchBar;
    
    
    UILabel *new =[[UILabel alloc]initWithFrame:CGRectMake(20, 80, 120, 18)];
    UIView  *view=[[UIView alloc]initWithFrame:CGRectMake(0, 110, MAINWIDTH, 4)];
    UILabel *new2 =[[UILabel alloc]initWithFrame:CGRectMake(20, imageY+DIAMETER+240, 120, 18)];
    UIView  *view2=[[UIView alloc]initWithFrame:CGRectMake(0, imageY+DIAMETER+270, MAINWIDTH, 4)];
    


    
    int totalCol = 3;
    CGFloat viewW = DIAMETER;
    CGFloat viewH = viewW;
    
    CGFloat marginX = (self.view.bounds.size.width - totalCol * viewW) / (totalCol + 1);
    CGFloat marginY = 50;
    CGFloat startY  = 80;
    
    for (int i = 0; i < 6; i++)
    {

        int row = i / totalCol;
        int col = i % totalCol;
        
        musicanInfo *musican = self.musicanList[i];
        CGFloat x = marginX + (viewW + marginX) * col;
        CGFloat y = startY + marginY + (viewH + marginY) * row;
        UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(x, y, viewW, viewH)];
        imageView.layer.masksToBounds =YES;
        imageView.backgroundColor =MTColor(220.0, 220.0, 220.0, 1.0);
        imageView.layer.cornerRadius =DIAMETER/2;
        imageView.image = musican.image;
        
        UILabel *name =[[UILabel alloc] initWithFrame:CGRectMake((DIAMETER+DIS)*col, y+DIAMETER+10, DIAMETER+DIS*2,16)];
        name.textAlignment = UITextAlignmentCenter;
        name.font = [UIFont systemFontOfSize:16];
        name.textColor = [UIColor blackColor];
        if([musican.other isEqual:@""])
        {
            name.text = musican.name;
            
        }
        else
        {
            NSString *string = [[NSString alloc] initWithFormat:@"（%@）", musican.other];
            name.text = [musican.name stringByAppendingString:string];
        }
        

        UILabel *music =[[UILabel alloc] initWithFrame:CGRectMake((DIAMETER+DIS)*col, y+DIAMETER+30,DIAMETER+DIS*2,16)];
        music.textAlignment = UITextAlignmentCenter;
        music.font = [UIFont systemFontOfSize:16];
        music.textColor = [UIColor blackColor];
        music.text = musican.music[0];
        
     
        [self.view addSubview:name];
        [self.view addSubview:music];
        [self.view addSubview:imageView];
        
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    CGRect buttonframe =CGRectMake(DIS,imageY+DIAMETER+200, MAINWIDTH-DIS*2, 18);
    button.frame = buttonframe;
    
    // 按钮边框宽度
    button.layer.borderWidth = 1.0;
    // 设置圆角
    button.layer.cornerRadius = 4.5;
    // 设置颜色空间为rgb，用于生成ColorRef
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0.8, 0.8, 0.8, 1 });
    // 设置边框颜色
    button.layer.borderColor = borderColorRef;
    
    new.text = @"最新制作人";
    new2.text = @"最新动态";
    
    
    [button setTitle:@"显示全部" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
    
    view.backgroundColor = MTColor(220.0, 220.0, 220.0, 1.0);
    view2.backgroundColor = MTColor(220.0, 220.0, 220.0, 1.0);

    
    [self.view addSubview:new];
    [self.view addSubview:view];
    [self.view addSubview:new2];
    [self.view addSubview:view2];



    [self.view addSubview:button];

    

    
}

-(void) butClick {

        NewViewController *new = [[NewViewController alloc]init];
        [self.navigationController pushViewController:new animated:YES];
   
}

- (IBAction)TextField_DidEndOnExit:(id)sender {
    // 隐藏键盘.
    UITextField *field =(UITextField *)[self.view viewWithTag:101];
    [field resignFirstResponder];}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITextField *field =(UITextField *)[self.view viewWithTag:101];
    [field resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
